﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using tuwien.auto.eibclient;

namespace KNXFramework.KNXObjects.ValueObjects.Signed
{
    internal class S16ObjectValue : IntObjectValue
    {
        public S16ObjectValue(string value)
            : base(value)
        {
            if (m_Value > 32767 || m_Value < -32768)
                throw new ApplicationException("S16ObjectValue: value must be between -32768 and 32767. Val : " + value);
        }

        public S16ObjectValue(EIBBuffer buffer)
        {
            int newValue = (buffer.data[2] << 8) | buffer.data[3];

            if (newValue > 32767)
                newValue -= 65536;
        }

        public override void AdaptBufferBeforeSending(bool isWrite, EIBBuffer buffer)
        {
            byte[] b = new byte[4];
            b[0] = buffer.data[0];
            b[1] = buffer.data[1];
            b[2] = (byte)((m_Value & 0xff00)>>8);
            b[3] = (byte)(m_Value & 0xff);
            buffer.data = b;
        }
    }
}
